package com.zzh.arc;


import android.app.Activity;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zzh.dialog.R;
import com.zzh.dialog.databinding.ChooserLayoutBinding;

import java.util.Collections;
import java.util.List;

/**
 * Created by zzh on 2024/9/12.
 *
 * @date: 2024/9/12 15:21
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.arc
 */
public class Chooser extends ListActivity {
    private AppAdapter adapter = null;
    ChooserLayoutBinding bind;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind = ChooserLayoutBinding.inflate(getLayoutInflater());
        setContentView(bind.getRoot());

        PackageManager pm = getPackageManager();
        Intent main = new Intent(Intent.ACTION_MAIN, null);

        main.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> launchables = pm.queryIntentActivities(main, 0);
        Collections.sort(launchables,
                new ResolveInfo.DisplayNameComparator(pm));

        adapter = new AppAdapter(pm, launchables);
        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v,
                                   int position, long id) {

        ResolveInfo launchable = adapter.getItem(position);
        ActivityInfo activity = launchable.activityInfo;
        ComponentName name = new ComponentName(activity.applicationInfo.packageName,
                activity.name);

        Intent getIntent = getIntent();
        Intent returnIntent = new Intent();
        returnIntent.putExtra("MESSAGE_package_name", name.getPackageName());
        returnIntent.putExtra("MESSAGE_position", getIntent.getIntExtra("MESSAGE_position", 0));
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }

    class AppAdapter extends ArrayAdapter<ResolveInfo> {
        private PackageManager pm = null;

        AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
            super(Chooser.this, R.layout.row, apps);
            this.pm = pm;
        }

        @Override
        public View getView(int position, View convertView,
                            ViewGroup parent) {
            if (convertView == null) {
                convertView = newView(parent);
            }

            bindView(position, convertView);

            return (convertView);
        }

        private View newView(ViewGroup parent) {
            return (getLayoutInflater().inflate(R.layout.row, parent, false));
        }

        private void bindView(int position, View row) {
            TextView label = row.findViewById(R.id.label);

            label.setText(getItem(position).loadLabel(pm));

            ImageView icon = row.findViewById(R.id.icon);

            icon.setImageDrawable(getItem(position).loadIcon(pm));
        }
    }
}